/**
 * pagination - jQuery xui
 *
 * Licensed under the GPL:
 *   http://www.gnu.org/licenses/gpl.txt
 *
 * Copyright 2015 xjb [ beymy.en@gmail.com ]
 *
 */
(function($) {
    /**
     * 绑定事件
     */
    function bindEvents(target) {
        var opts = $.data(target, 'pagination').options;
        var pager = $(target);

        $('a.x-pagination-first', pager).unbind('.pagination').bind('click.pagination', function() {
            if (opts.pageNumber > 1) {
                selectPage(target, 1);
            }
        });
        $('a.x-pagination-prev', pager).unbind('.pagination').bind('click.pagination', function() {
            if (opts.pageNumber > 1) {
                selectPage(target, opts.pageNumber - 1);
            }
        });
        $('a.x-pagination-next', pager).unbind('.pagination').bind('click.pagination', function() {
            var pageCount = Math.ceil(opts.total / opts.pageSize);
            if (opts.pageNumber < pageCount) {
                selectPage(target, opts.pageNumber + 1);
            }
        });
        $('a.x-pagination-last', pager).unbind('.pagination').bind('click.pagination', function() {
            var pageCount = Math.ceil(opts.total / opts.pageSize);
            if (opts.pageNumber < pageCount) {
                selectPage(target, pageCount);
            }
        });
        $('a.x-pagination-reload', pager).unbind('.pagination').bind('click.pagination', function() {
            selectPage(target, opts.pageNumber);
        });
        
        $('select.x-pagination-page-list', pager).unbind('.pagination').bind('change.pagination', function() {
            opts.pageSize = $(this).val();
            selectPage(target, opts.pageNumber);
        });
    }

    function selectPage(target, pageNumber) {
        var opts = $.data(target, 'pagination').options;
        var pageCount = Math.ceil(opts.total / opts.pageSize);
        if (pageNumber > pageCount) pageNumber = pageCount;
        if (pageNumber < 1) pageNumber = 1; //页码最小是1
        opts.onSelectPage.call(target, pageNumber, opts.pageSize);
        opts.pageNumber = pageNumber;
        showPagerInfo(target);
    }

    function showPagerInfo(target) {
        var opts = $.data(target, 'pagination').options;
        var pager = $(target);
        var pageCount = Math.ceil(opts.total / opts.pageSize);

        if (pageCount < 1) { //总页码数最小是1
            pageCount = 1;
        }
        $('.x-pagination-info', pager).text(opts.displayMsg.replace(/{page}/, opts.pageNumber).replace(/{pages}/, pageCount).replace(/{total}/, opts.total));
        $('.x-pagination-first,.x-pagination-prev', pager).parent().removeClass('disabled').addClass((opts.pageNumber == 1) ? 'disabled' : '');
        $('.x-pagination-next,.x-pagination-last', pager).parent().removeClass('disabled').addClass((opts.pageNumber == pageCount) ? 'disabled' : '');
    }

    $.fn.pagination = function(options, param) {
        if (typeof options == 'string') {
            return $.fn.pagination.methods[options](this, param);
        }

        options = options || {};
        return this.each(function() {
            var state = $.data(this, 'pagination');
            var opts;
            if (state) {
                opts = $.extend(state.options, options);
            } else {
                opts = $.extend({}, $.fn.pagination.defaults, $.fn.pagination.parseOptions(this), options);
                $.data(this, 'pagination', {
                    options: opts
                });
            }
            bindEvents(this);
            showPagerInfo(this);
        });
    };

    $.fn.pagination.methods = {
        options: function(jq) {
            return $.data(jq[0], "pagination").options;
        },
        select: function(jq, param) {
            return jq.each(function() {
                selectPage(this, param);
            });
        }
    };

    $.fn.pagination.parseOptions = function(target) {
        return $.extend({}, $.parser.parseOptions(target));
    };

    $.fn.pagination.defaults = {
        total: 0,
        pageSize: 10,
        pageNumber: 1,
        displayMsg: '第{page}页/共{pages}页,共{total}条记录',
        onSelectPage: function(pageNumber, pageSize) {},
        onChangePageSize: function(pageSize) {}
    };
})(jQuery);
